//
//  BaseVC.swift
//  Moonlight
//
//  Created by Penlon Kim on 2023/6/5.
//  Copyright © 2023 Moonlight Game Streaming Project. All rights reserved.
//

import UIKit
import LXFProtocolTool
import ESTabBarController_swift
import EachNavigationBar

class BaseVC: UIViewController {
    
    deinit {
        PPP("[\(NSStringFromClass(type(of: self)))]>>>已被释放")
        self.removeNOC()
    }
    
    var backBarButtonItemHidden: Bool {
        return false
    }
    
    @available(iOS 13.0, *)
    override var overrideUserInterfaceStyle: UIUserInterfaceStyle {
        get { return DarkModeTool.mode.style }
        set {  }
    }
    
    // MARK: Lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .lightWhiteDark27
        
        if !backBarButtonItemHidden {
            navigation.item.leftBarButtonItem = UIBarButtonItem(image: .ArrowFork.icon_leftArrow, style: .done, target: self, action: #selector(goBack))
        }
        
        self.addNOC()
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.bk_showCustomTabBar()
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.bk_setPopSwipe(true)
    }
    
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        
    }
    
    // MARK: - lazy
    lazy var backBtn: UIButton = {
        let btn = UIButton()
        btn.setImage(.ArrowFork.icon_leftArrow, for: .normal)
        btn.bk_setEnlargeEdge(10)
        btn.bk_addTarget { [unowned self] (sender) in
            self.bk_autoBack()
        }
        return btn
    }()
    
}

// MARK: - Selector
extension BaseVC {
    
    @objc func goBack() {
        self.bk_autoBack()
    }
    
}

// MARK: - 通知
extension BaseVC {
    
    private func addNOC() {
        
    }
    
    private func removeNOC() {
        
    }
    
}

// MARK: - Public
extension BaseVC {
    
    func bk_showCustomTabBar() {
        guard let tabBar = self.tabBarController as? BaseTabBarVC else { return }
        if !tabBar.tabBar.isHidden {
            tabBar.showCustomTabBar()
        }
    }
    
    /// 是否支持手势侧滑返回
    func bk_setPopSwipe(_ isEnabled: Bool) {
        if self.navigationController != nil {
            self.navigationController?.interactivePopGestureRecognizer?.isEnabled = isEnabled
        }
    }
    
    func bk_autoBack() {
        guard let viewCtrls = self.navigationController?.viewControllers else {
            self.dismiss(animated: true, completion: nil)
            return
        }
        if viewCtrls.count > 1 {
            if self.navigationController?.topViewController == self {
                self.navigationController?.popViewController(animated: true)
            }
        } else {
            self.dismiss(animated: true, completion: nil)
        }
    }
    
    /// 模态弹出视图
    func bk_present(_ viewControllerToPresent: UIViewController,
                    type: UIModalPresentationStyle = .fullScreen,
                    animated flag: Bool,
                    completion: (() -> Void)? = nil) {
        
        if #available(iOS 13.0, *) {
            viewControllerToPresent.modalPresentationStyle = type
            viewControllerToPresent.isModalInPresentation = true
        } else {
            viewControllerToPresent.modalPresentationStyle = type
        }
        super.present(viewControllerToPresent, animated: flag, completion: completion)
        
    }
    
    /// 无感知的移除当前控制器并打开新的控制器
    func bk_silencePushViewController(toVc: UIViewController) {
        if var vcs = self.navigationController?.viewControllers {
            vcs.append(toVc)
            vcs.remove(at: vcs.count - 2)
            self.navigationController?.setViewControllers(vcs, animated: true)
        }
    }
    
}

// MARK: - 导航栏扩展UIViewController
extension UIViewController {
    
    /// 改变导航栏的透明度
    /// - Parameter clear: 是否是变透明
    func bk_changeNaviBarAnimate(_ clear: Bool) {
        UIView.animate(withDuration: 0.5) {
            self.navigation.bar.alpha = clear ? 0 : 1.0
        }
    }
    
    /// 隐藏返回按钮
    /// - Parameter isHidden: 是否隐藏
    func bk_setBackBarButtonItem(isHidden: Bool) {
        navigation.bar.backBarButtonItem = isHidden ? nil : .init(style: .image(.ArrowFork.icon_leftArrow))
    }
    
    /// 配置导航栏字体
    /// - Parameter color: 颜色
    func bk_naviBarTitleColor(color: UIColor) {
        navigation.bar.titleTextAttributes = [
            .foregroundColor: color,
            .font: UIFont.systemFont(ofSize: 16, weight: .bold)
        ]
    }
    
}
